home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / ColorSync SDK / Sample Code / CSDemo 2.1 / ShellSources / appMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-13  |  14.0 KB  |  490 lines  |  [TEXT/CWIE]

  1. // Simple framework for Macintosh sample code
  2. //
  3. // David Hayward and Nick Thompson 
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyrite 1994, Apple Computer,Inc
  8. //
  9. // Application event dispatching code.
  10. // Most of the stuff in here will not need altering from app to app.
  11. // 
  12. // 9/13/94    nick    first cut
  13. // 12/13/94    david    several modifications
  14.  
  15.  
  16. #include <LowMem.h>
  17. #include <Windows.h>
  18. #include <ToolUtils.h>
  19. #include <Errors.h>
  20. #include <AppleEvents.h>
  21. #include <QDOffscreen.h>
  22. #include <SegLoad.h>
  23. #include <DiskInit.h>
  24. #include <Dialogs.h>
  25.  
  26. #include "aeUtils.h"
  27. #include "dragUtils.h"
  28.  
  29. #include "appGlobals.h"
  30. #include "appMain.h"
  31. #include "appMenus.h"
  32. #include "appAEvts.h"
  33. #include "appErrors.h"
  34.  
  35. #include "win.h"
  36. #include "winTables.h"
  37.  
  38. #ifndef PIGS_SHELL_NOPRINT
  39. #include "gxGlobals.h"
  40. #include "gxUtils.h"
  41. #include "gxPrintUtils.h"
  42. #endif
  43.  
  44.  
  45. /**\
  46. |**| ==============================================================================
  47. |**| PRIVATE FUNCTION PROTOTYPES
  48. |**| ==============================================================================
  49. \**/
  50. void    InitToolbox            ( void ) ;
  51. void    InitMenuBar            ( void ) ;
  52. void    DoEventLoop            ( void ) ;
  53. void    DoMouseDownEvent    ( EventRecord *event ) ;
  54. void    DoKeyDownEvent        ( EventRecord *event ) ;
  55. void    DoUpdateEvent        ( EventRecord *event ) ;
  56. void    DoDiskEvent            ( EventRecord *event ) ;
  57. void    DoActivateEvent        ( EventRecord *event ) ;
  58. void    DoOSEvent            ( EventRecord *event ) ;
  59. void    DoNullEvent            ( EventRecord *event ) ;
  60.  
  61.  
  62. /**\
  63. |**| ==============================================================================
  64. |**| PUBLIC FUNCTIONS
  65. |**| ==============================================================================
  66. \**/
  67.  
  68.  
  69. /*------------------------------------------------------------------------------*\
  70.     DoEvent
  71.  *------------------------------------------------------------------------------*
  72.         This routine dispaches one event record.
  73.         This routine is called by DoEventLoop() and also by the
  74.         GX Printing Event Handler.
  75. \*------------------------------------------------------------------------------*/
  76. void DoEvent ( EventRecord *event )
  77. {
  78.     switch (event->what)
  79.     {
  80.         case nullEvent :                        /* handle null events */
  81.             DoNullEvent( event ) ;
  82.             break ;
  83.         
  84.         case mouseDown :                        /* handle mouse clicks */
  85.             DoMouseDownEvent( event ) ;
  86.             break ;
  87.         
  88.         case mouseUp :
  89.             break ;
  90.         
  91.         case keyDown :                            /* handle key hits */
  92.         case autoKey :
  93.             DoKeyDownEvent( event ) ;
  94.             break ;
  95.             
  96.         case updateEvt :                        /* handle update events */
  97.             DoUpdateEvent( event ) ;
  98.             break ;
  99.         
  100.         case diskEvt:                            /* handle disk events */
  101.             DoDiskEvent( event ) ;
  102.             break ;
  103.  
  104.         case activateEvt:                        /* handle activate events */
  105.             DoActivateEvent( event ) ;
  106.             break ;
  107.  
  108.         case osEvt:                                /* handle os events */
  109.             DoOSEvent( event ) ;
  110.             break ;
  111.  
  112.         case kHighLevelEvent:                    /* handle Apple Events */
  113.             AEProcessAppleEvent( event ) ;
  114.             break ;
  115.     }
  116. }
  117.  
  118.  
  119. /*------------------------------------------------------------------------------*\
  120.     main
  121.  *------------------------------------------------------------------------------*
  122.         the main routine which initialized the various managers and
  123.         starts the main event loop.
  124. \*------------------------------------------------------------------------------*/
  125. void main ( void )
  126. {
  127.     InitToolbox() ;
  128.     InitMenuBar() ;
  129.     
  130.     gAppResRefNum = CurResFile() ;
  131.  
  132.     // check for AppleEvents
  133.     if ( AE_initialize() != noErr )
  134.         FailIfErr( eFatalNeedsAEVTMgr ) ;
  135.     FailIfErr( RegisterAppleEvents() ) ;
  136.     
  137. #ifndef PIGS_SHELL_NOPRINT
  138.     // check for GX
  139.     (void) QDGX_initialize() ;
  140.     (void) GXPrinting_initialize() ;
  141. #endif
  142.  
  143. #ifndef PIGS_SHELL_NODRAG
  144.     // check for Drag&Drop
  145.     (void) Drag_initialize() ;
  146. #endif
  147.  
  148.     // run
  149.     DoEventLoop() ;
  150.     
  151. #ifndef PIGS_SHELL_NOPRINT
  152.     // if necessary, clean up after GX
  153.     DisposeGXPrinting() ;
  154.     DisposeGXStuff() ;
  155. #endif
  156. }
  157.  
  158.  
  159. /**\
  160. |**| ==============================================================================
  161. |**| PRIVATE FUNCTIONS
  162. |**| ==============================================================================
  163. \**/
  164.  
  165.  
  166. /*------------------------------------------------------------------------------*\
  167.     InitToolbox
  168.  *------------------------------------------------------------------------------*
  169.         This routine initializes the standard managers.
  170.         This routine is called by main().
  171. \*------------------------------------------------------------------------------*/
  172. static void InitToolbox ( void )
  173. {
  174.     MaxApplZone() ;
  175.  
  176.     InitGraf( (Ptr)&qd.thePort ) ;
  177.     InitFonts() ;
  178.     InitWindows() ;
  179.     InitMenus() ;
  180.     FlushEvents( everyEvent, 0 ) ;
  181.     TEInit() ;
  182.     InitDialogs(0) ;
  183.     InitCursor() ;
  184.  
  185.     MoreMasters() ; MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  186.     MoreMasters() ; MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  187.     MoreMasters() ; MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  188. }
  189.  
  190.  
  191. /*------------------------------------------------------------------------------*\
  192.     InitMenuBar
  193.  *------------------------------------------------------------------------------*
  194.         This routine reads in the MBAR resource, adds the DA to the apple 
  195.         menu, and sets up all the menu states by calling DoAppAdjustMenus.
  196.         This routine is called by main().
  197. \*------------------------------------------------------------------------------*/
  198. static void InitMenuBar ( void )
  199. {
  200.     OSErr         retCode = noErr ;
  201.     Handle        menuBar = nil;
  202.     short        i ;
  203.     
  204.     menuBar = GetNewMBar( mMenuBar ) ;        // Read menus into menu bar, MBAR res id is 128
  205.     if ( menuBar == nil )                    // if we dont have it then quit 
  206.     {                                        // your app needs a dialog here, maybe.
  207.         SysError( dsSysErr ) ;                 // Thing is, if it couldn't get the menu bar,
  208.         ExitToShell() ;                     // it may not get the dialog resource.  
  209.     }                                         // I call Syserror here.  This way I
  210.                                              // know there has been a problem
  211.  
  212.     SetMenuBar( menuBar ) ;                    // Install menus
  213.     DisposeHandle( menuBar ) ;
  214.     
  215.     if (mFirstSubMenu && mLastSubMenu)
  216.         for (i=mFirstSubMenu; i<=mLastSubMenu; i++)
  217.             InsertMenu( GetMenu(i), hierMenu );
  218.  
  219.     AppendResMenu( GetMenuHandle(mApple),'DRVR');    // Add DA names to Apple menu, ID 128
  220.  
  221. #ifndef PIGS_SHELL_NOPRINT
  222.     gQDGXEditMenuRec.editMenuID = mEdit ;    // fill in the GXEditMenuRec so that print
  223.     gQDGXEditMenuRec.cutItem    = iCut ;    // dialogs can, do cut, copy, and paste
  224.     gQDGXEditMenuRec.copyItem   = iCopy ;    // This global is used by GXStyleDialog
  225.     gQDGXEditMenuRec.pasteItem  = iPaste ;    // and GXJobDialog
  226.     gQDGXEditMenuRec.clearItem  = iClear ;
  227.     gQDGXEditMenuRec.undoItem   = iUndo ;
  228. #endif
  229.  
  230.     DoAppAdjustMenus() ;
  231. }
  232.  
  233.  
  234. /*------------------------------------------------------------------------------*\
  235.     DoEventLoop
  236.  *------------------------------------------------------------------------------*
  237.         This routine processes events until gQuitFlag gets set.
  238.         This routine is called by main().
  239. \*------------------------------------------------------------------------------*/
  240. static void DoEventLoop ( void )
  241. {
  242.     EventRecord event ;
  243.  
  244.     while ( !gQuitFlag ) 
  245.     {
  246.         if ( WaitNextEvent( everyEvent, &event, gAppSleepTicks, nil ) )
  247.             DoEvent( &event ) ;
  248.         else
  249.             DoNullEvent( &event ) ;
  250.     }
  251. }
  252.  
  253.  
  254. /*------------------------------------------------------------------------------*\
  255.     DoMouseDownEvent
  256.  *------------------------------------------------------------------------------*
  257.         This routine handles mouse down events.
  258.             inDrag clicks are handled here,
  259.             inContent clicks are sent to the window's ClickProcPtr,
  260.             inGoAway clicks are sent to the window's CloseProcPtr
  261.             inMenuBar clicks are handled by calling HandleMenuCommand(),
  262.             inGrow clicks are handled by resizing the window and calling its ResizeProcPtr,
  263.             inZoom clicks are handled by zooming the window and calling its ResizeProcPtr,
  264.         This routine is called by DoEvent().
  265. \*------------------------------------------------------------------------------*/
  266. static void DoMouseDownEvent ( EventRecord *event )
  267. {
  268.     WindowRef        window ;
  269.     winHandle        win ;
  270.     short            clickArea ;
  271.     Rect            screenRect ;
  272.     Rect            sizeRect ;
  273.     long            sizeHV ;
  274.     RgnHandle        grayRgn ;
  275.  
  276.     clickArea = FindWindow( event->where, &window ) ;
  277.     win = GetWindowWinHandle( window ) ;
  278.     
  279.     switch ( clickArea )
  280.     {
  281.         case inDrag:
  282.             grayRgn = LMGetGrayRgn() ;
  283.             screenRect = (**grayRgn).rgnBBox ;
  284.             SelectWindow( window ) ;
  285.             DragWindow( window, event->where, &screenRect ) ;
  286.             break ;
  287.  
  288.         case inContent:
  289.             if ( window == FrontWindow() )
  290.                 CallWinClickProc( win, event ) ;
  291.             else
  292.                 SelectWindow( window ) ;
  293.             break ;
  294.         
  295.         case inGoAway:
  296.             if ( TrackGoAway( window, event->where ) )
  297.             {
  298.                 CallWinCloseProc( win ) ;
  299.                 DoAppAdjustMenus() ;
  300.             }
  301.             break;
  302.  
  303.         case inMenuBar: 
  304.             HandleMenuCommand( MenuSelect(event->where) ) ;
  305.             break;
  306.  
  307.         case inGrow:
  308.             if ( win != nil )
  309.             {
  310.                 sizeRect = GetWinSizeRect( win ) ;
  311.                  sizeHV = GrowWindow( window, event->where, &sizeRect ) ;
  312.                 if ( sizeHV != 0L )            // if the window need to be resized
  313.                 {
  314.                     SizeWindow( window, LoWord(sizeHV), HiWord(sizeHV), true ) ;
  315.                     CallWinResizeProc( win ) ;
  316.                 }
  317.             }
  318.             break;
  319.  
  320.         case inZoomIn: 
  321.         case inZoomOut: 
  322.             if ( TrackBox( window,event->where,clickArea) )
  323.             {
  324.                 SetPort( (GrafPtr)window ) ;
  325.                 EraseRect( &(((CGrafPtr)window)->portRect) ) ;
  326.                 ZoomWindow( window, clickArea, true) ;
  327.                 CallWinResizeProc( win ) ;
  328.             }
  329.             break;
  330.  
  331.         default:
  332.             break ;
  333.     }
  334. }
  335.  
  336.  
  337. /*------------------------------------------------------------------------------*\
  338.     DoKeyDownEvent
  339.  *------------------------------------------------------------------------------*
  340.         This routine handles key down events.
  341.         If the command key is down then the event is handled by calling HandleMenuCommand(),
  342.         Otherwize, the event is sent to the frontmost window by calling its KeyProcPtr,
  343.         This routine is called by DoEvent().
  344. \*------------------------------------------------------------------------------*/
  345. static void DoKeyDownEvent ( EventRecord *event )
  346. {
  347.     char            key ;
  348.     winHandle        win ;
  349.  
  350.     key = event->message & charCodeMask ;
  351.     
  352.     if ( event->modifiers & cmdKey )             /* Command key down? */
  353.         HandleMenuCommand( MenuKey(key) ) ;
  354.     else
  355.     {
  356.         // see if the frontmost window can handle the event
  357.         win = GetWindowWinHandle( FrontWindow() ) ;
  358.         CallWinKeyProc( win, event ) ;
  359.     }
  360. }
  361.  
  362.  
  363. /*------------------------------------------------------------------------------*\
  364.     DoUpdateEvent
  365.  *------------------------------------------------------------------------------*
  366.         This routine handles update events.
  367.         The event is sent to the appropriate window by calling its UpdateProcPtr,
  368.         This routine is called by DoEvent().
  369. \*------------------------------------------------------------------------------*/
  370. static void DoUpdateEvent ( EventRecord *event )
  371. {
  372.     WindowRef       window ;
  373.     winHandle        win  ;
  374.  
  375.     window = (WindowRef) event->message ;
  376.     win = GetWindowWinHandle( window ) ;
  377.     CallWinUpdateProc( win, event ) ;
  378. }
  379.  
  380.  
  381. /*------------------------------------------------------------------------------*\
  382.     DoDiskEvent
  383.  *------------------------------------------------------------------------------*
  384.         This routine handles disk events.
  385.         It is called by DoEvent().
  386. \*------------------------------------------------------------------------------*/
  387. static void DoDiskEvent ( EventRecord *event )
  388. {
  389.     Point aPoint = {100, 100} ;
  390.     if ( HiWrd(event->message) != noErr ) 
  391.         (void) DIBadMount(aPoint, event->message) ;
  392. }
  393.  
  394.  
  395. /*------------------------------------------------------------------------------*\
  396.     DoActivateEvent
  397.  *------------------------------------------------------------------------------*
  398.         This routine handles activate events.  Whenever a window becomes active,
  399.         it sets up all the menu states by calling DoAppAdjustMenus.  The update
  400.         event is then forwarded to the appropriate window by calling its
  401.         ActivateProcPtr.
  402.         This routine is called by DoEvent().
  403. \*------------------------------------------------------------------------------*/
  404. static void DoActivateEvent ( EventRecord *event )
  405. {
  406.     WindowRef       window ;
  407.     winHandle        win ;
  408.     Boolean            becomingActive ;
  409.  
  410.     becomingActive = (event->modifiers) & activeFlag ;
  411.     window = (WindowRef)event->message ;
  412.     win = GetWindowWinHandle( window ) ;
  413.  
  414.     if (becomingActive)
  415.     {
  416.         DoAppAdjustMenus() ;                    // undim menu items
  417.         SetGWorld( (CGrafPtr)window, nil ) ;    // set the window to be the current port
  418.     }
  419.     CallWinActivateProc( win, becomingActive ) ;
  420. }
  421.  
  422.  
  423. /*------------------------------------------------------------------------------*\
  424.     DoOSEvent
  425.  *------------------------------------------------------------------------------*
  426.         This routine handles os events such as Suspend/Resume and MouseMoved
  427.         On Suspend/Resume the gInBackground global and the GXJob are updated. 
  428.         Also, the frontmost window is notified by calling its ActivateProcPtr
  429.         This routine is called by DoEvent().
  430. \*------------------------------------------------------------------------------*/
  431. static void DoOSEvent ( EventRecord *event )
  432. {
  433.     WindowRef       window ;
  434.     winHandle        win ;
  435.     Boolean            becomingActive ;
  436.  
  437.     switch ((event->message >> 24) & 0x00FF)         // High byte of message
  438.     {
  439.         case suspendResumeMessage:
  440.  
  441.             becomingActive = (event->message & resumeFlag ) ;
  442.             gInBackground = !becomingActive ;
  443.  
  444.             (void) CallAllWinResumeProcs( becomingActive ) ;
  445.  
  446. #ifndef PIGS_SHELL_NOPRINT
  447.             // check to see if GX is installed, if so then make sure we 
  448.             // update the job object for each window
  449.             if( becomingActive && GXPrinting_present()==noErr ) 
  450.             {
  451.                 gxJob job ;
  452.                 win = FrontWin() ;
  453.                 while ( !win ) {
  454.                     job = GetWinGXJob( win ) ;
  455.                     if ( job ) GXUpdateJob( job ) ;
  456.                     win = GetNextWin(win) ;
  457.                 }
  458.             }
  459. #endif
  460.  
  461.             window = FrontWindow() ;
  462.             win = GetWindowWinHandle( window ) ;
  463.             CallWinActivateProc( win, becomingActive ) ;
  464.             if (becomingActive)
  465.             {
  466.                 DoAppAdjustMenus() ;                    // undim menu items
  467.                 if ( window )
  468.                     SetGWorld( (CGrafPtr)window, nil ) ;// set the window to be the current port
  469.             }
  470.  
  471.             break ;
  472.     }
  473. }
  474.  
  475.  
  476. /*------------------------------------------------------------------------------*\
  477.     DoNullEvent
  478.  *------------------------------------------------------------------------------*
  479.         This routine handles null events.
  480.         The event is sent to all windows by calling each NullProcPtr.
  481.         This routine is called by DoEvent().
  482. \*------------------------------------------------------------------------------*/
  483. static void DoNullEvent ( EventRecord *event )
  484. {
  485.     (void) CallAllWinNullProcs( event );
  486. }
  487.  
  488.  
  489.  
  490.